package com.imy.rootserver;

import android.app.UiAutomation;
import android.content.Context;
import android.os.PowerManager;
import android.os.RemoteException;
import android.util.Log;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.uiautomator.Configurator;
import androidx.test.uiautomator.UiDevice;

import com.imy.rootserver.constant.Constant;
import com.imy.rootserver.gesture.TaskHandler;
import com.imy.rootserver.socket.SocketTransceiver;
import com.imy.rootserver.socket.TcpServer;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.concurrent.CountDownLatch;

@RunWith(AndroidJUnit4.class)
public class TouchServer {
  private String TAG = "TouchServer";
  private static final String WAKE_LOCK_TAG = "UiAutomator2:ScreenKeeper";

  private PowerManager.WakeLock wakeLock;
  private TcpServer server;
  private TaskHandler mTaskHandler;
  private CountDownLatch latch = new CountDownLatch(1);
  private PowerManager powerManager;
  private static final long MAX_TEST_DURATION = 24 * 60 * 60 * 1000;
  @Test
  @RootRequired
  public void useAppContext() throws Exception {
    //同进程app拥有无障碍服务时 UiAutomation默认也拥有此服务 将会覆盖app的 增加此属性
    int flags = UiAutomation.FLAG_DONT_SUPPRESS_ACCESSIBILITY_SERVICES;
    Configurator.getInstance().setUiAutomationFlags(flags);
    server = new TcpServer(Constant.JUNIT_PORT) {
      @Override
      public void onConnect(SocketTransceiver client) {
        Log.i(TAG, "onConnect");

      }
      @Override
      public void onConnectFailed(String message) {
        Log.i(TAG, "onConnectFailed:"+message);
      }
      @Override
      public void onReceive(SocketTransceiver client, String s) {
         Log.i(TAG,"onReceive s:"+s);
          mTaskHandler.handTask(s);
      }
      @Override
      public void onDisconnect(SocketTransceiver client) {
        Log.i(TAG, "onDisconnect");

      }
      @Override
      public void onServerStop() {
        //关闭了
        Log.i(TAG, "onDisconnect");
        latch.countDown();
      }
    };
    mTaskHandler= new TaskHandler(server);
    Log.i(TAG, "server  start");
    Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
    powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    acquireWakeLock();
    server.start();
    try {
      latch.await();

    } catch (Exception e) {

      server.stop();
    }

  }
  private UiDevice uiDevice;

  private void acquireWakeLock() {
    releaseWakeLock();
    wakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, WAKE_LOCK_TAG);
    try {
      wakeLock.acquire(MAX_TEST_DURATION);
      uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
      uiDevice.wakeUp();
    } catch (SecurityException e) {
      Log.e(TAG, "Security Exception" + e);
    } catch (RemoteException e) {
      Log.e(TAG, "RemoteException " + e);
      e.printStackTrace();
    }
  }

  private void releaseWakeLock() {
    if (wakeLock == null) {
      return;
    }

    try {
      wakeLock.release();
    } catch (Exception e) {/* ignore */}
    wakeLock = null;
  }
}
